package com.ss.android.crash.log;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import inline.CrashDemo;
import inline.PackageUtils;

public final class c {
    public a a;
    public HandlerThread b;
    public b c;
    public d d;
    public volatile boolean e;
    private final Context f;
    private volatile long g;
    private final SharedPreferences h;
    private static volatile boolean i;
    private static List j;

    static {
        i = false;
        j = new ArrayList();
    }

    public c(Context arg5) {

        this.b = null;
        this.e = false;
        if (arg5 == null) {
            throw new IllegalArgumentException("context is not be null");
        }

        this.f = arg5.getApplicationContext();
        this.h = this.f.getSharedPreferences("anr_monitor_table", 0);
        this.g = this.h.getLong("trace_anr_happen_time", 0);
    }

    public static void a() {
        i = true;
    }

    private static String a(BufferedReader arg3) throws IOException {
        StringBuilder v0 = new StringBuilder();
        while (true) {
            String v1 = arg3.readLine();
            if (v1 != null && v1.trim().length() > 0) {
                v0.append(v1).append("\n");
                continue;
            }

            break;
        }

        return v0.toString();
    }

    private JSONObject a(String arg17, int arg18, String arg19) {
        String v13;
        int v6_2;
        String v7_1;
        String v5;
        int v4_2;
        Object[] v6_1;
        int v3_1;
        int v8;
        JSONArray v12;
        long v4_1;
        BufferedReader v9;
        JSONObject v2;
        if (TextUtils.isEmpty(((CharSequence) arg17))) {
            v2 = null;
            return v2;
        }

        File v2_1 = new File(arg17);
        if ((v2_1.exists()) && (v2_1.canRead())) {
            try {
                v9 = new BufferedReader(new FileReader(v2_1));
                Pattern v2_3 = Pattern.compile("-{5}\\spid\\s\\d+\\sat\\s\\d+-\\d+-\\d+\\s\\d{2}:\\d{2}:\\d{2}\\s-{5}");
                Pattern v10 = Pattern.compile("-{5}\\send\\s\\d+\\s-{5}");
                Pattern v3 = Pattern.compile("Cmd\\sline:\\s(\\S+)");
                Pattern v11 = Pattern.compile("\".+\"\\s(daemon\\s){0,1}prio=\\d+\\stid=\\d+\\s.*");
                SimpleDateFormat v4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
                Object[] v2_4 = a(v9, new Pattern[]{v2_3});
                if (v2_4 == null) {
                    return null;
                } else {
                    long v6 = Long.parseLong(v2_4[1].toString().split("\\s")[2]);
                    v4_1 = v4.parse(v2_4[1].toString().split("\\s")[4] + " " + v2_4[1].toString().split("\\s")[5]).getTime();
                    v2_4 = a(v9, new Pattern[]{v3});
                    if (v2_4 == null) {
                        return null;
                    } else {
                        String v2_5 = v2_4[1].toString().split("\\s")[2];
                        if (v6 == (((long) arg18)) && (v2_5.equalsIgnoreCase(arg19))) {
                            if (this.g != 0 && Math.abs(this.g - v4_1) < 20000) {
                                return null;
                            }

                            this.g = v4_1;
                            if (this.h != null) {
                                this.h.edit().putLong("trace_anr_happen_time", this.g).apply();
                            }

                            v2 = new JSONObject();
                            v2.put("anrTime", v4_1);
                            v12 = new JSONArray();
                            v8 = 0;
                            v3_1 = 0;
                            while (true) {
                                v6_1 = a(v9, new Pattern[]{v10, v11});
                                if (v6_1 != null && v6_1[0] == v11) {
                                    v4_2 = -1;
                                    v5 = "";
                                    Matcher v7 = Pattern.compile("\".+\"").matcher(v6_1[1].toString());
                                    if (v7.find()) {
                                        v7_1 = v7.group().substring(1, v7.group().length() - 1);
                                    } else {
                                        v7_1 = v5;
                                    }
                                    Matcher v5_1 = Pattern.compile("tid=\\d+").matcher(v6_1[1].toString());
                                    if (v5_1.find()) {
                                        String v4_3 = v5_1.group();
                                        v6_2 = Integer.parseInt(v4_3.substring(v4_3.indexOf("=") + 1));
                                    } else {
                                        v6_2 = v4_2;
                                    }
                                    v13 = a(v9);
                                    if (v6_2 == -1) {
                                        continue;
                                    }

                                    if (TextUtils.isEmpty(((CharSequence) v7_1))) {
                                        continue;
                                    }

                                    if (TextUtils.isEmpty(((CharSequence) v13))) {
                                        continue;
                                    }

                                    if (v7_1.equalsIgnoreCase("main")) {
                                        v2.put("mainStackFromTrace", v13);
                                        v3_1 = 1;
                                    }

                                    ++v8;
                                    long v14 = ((long) v8);
                                    if (i) {
                                        v4_1 = 500;
                                    } else {
                                        v4_1 = 5;
                                    }

                                    if (v14 > v4_1) {
                                    } else {
                                        JSONObject v4_4 = new JSONObject();
                                        v4_4.put("id", v6_2);
                                        v4_4.put("name", v7_1);
                                        v4_4.put("stack", v13);
                                        v12.put(v4_4);
                                    }
                                    continue;
                                } else {
                                    v2.put("allThreadStack", v12);
                                    if (v3_1 == 0) {
                                        v2.put("mainStackFromTrace", b());
                                    }
                                    v2.put("thread_number", v8);
                                    return v2;
                                }
                            }
                        }
                        return null;
                    }
                }
            } catch (Exception v2_2) {
                v2_2.printStackTrace();
                v2 = null;
            }
        } else {
            v2 = null;
        }

        return v2;
    }

    private static Object[] a(BufferedReader arg7, Pattern[] arg8) throws IOException {
        Object[] v0 = null;
        if (arg8.length > 0) {
            while (true) {
                String v3 = arg7.readLine();
                if (v3 != null) {
                    int v4 = arg8.length;
                    int v1 = 0;
                    while (true) {
                        if (v1 >= v4) {
                            break;
                        }

                        Pattern v5 = arg8[v1];
                        if (v5.matcher(((CharSequence) v3)).matches()) {
                            v0 = new Object[]{v5, v3};
                        } else {
                            ++v1;
                            continue;
                        }

                        return v0;
                    }
                }
            }
        }

        return v0;
    }

    public final void a(int arg2, String arg3) {
        if (this.c != null) {
            this.c.obtainMessage(arg2, arg3).sendToTarget();
        }
    }

    public final void b(int arg7, String arg8) {
        k v0_4;
        JSONObject v1_1;
        int v2;
        String v1;
        JSONObject v0_1;
        try {
            if (this.f == null) {
                return;
            }

            v0_1 = new JSONObject();
            v1 = this.f.getPackageName();
            if (!PackageUtils.a(this.f, v1)) {
                return;
            }

            v2 = Process.myPid();
            if (TextUtils.isEmpty(((CharSequence) v1))) {
                return;
            }

            if (v2 <= 0) {
                return;
            }

            if (arg7 == 200) {
                v0_1 = this.a(arg8, v2, v1);
            } else {
                if (arg7 == 100) {
                    v0_1 = CrashDemo.a(i);
                }
            }


            if (v0_1 == null) {
                return;
            }

            if (v0_1.length() <= 0) {
                return;
            }

            v0_1.put("pid", v2);
            v0_1.put("package", v1);
            v0_1.put("is_remote_process", 0);
            v1_1 = new JSONObject();
            v1_1.put("data", v0_1.toString());
            v1_1.put("is_anr", 1);
            v1_1.put("anr_time", System.currentTimeMillis());
            String v0_2 = android.support.design.a.w(this.f);
            if (!TextUtils.isEmpty(((CharSequence) v0_2))) {
                v1_1.put("anr_info", v0_2);
            }

            if (this.f.getApplicationContext() != null) {
                CrashUtils.a(this.f, v1_1);
            }

            i v0_3 = com.ss.android.crash.log.i.b();
            if (v1_1.length() <= 0) {
            } else if (v0_3.b == null) {
            } else if (v0_3.a == null) {
            } else {
                v1_1.put("last_create_activity", v0_3.a.c);
                v1_1.put("last_resume_activity", v0_3.a.d);
                v1_1.put("app_start_time", v0_3.c);
                v1_1.put("app_start_time_readable", new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US).format(new Date(v0_3.c)));
                v1_1.put("alive_activities", v0_3.a.a());
                v1_1.put("running_task_info", v0_3.a.b());
                v0_4 = v0_3.b;
                if (v1_1.length() <= 0) {
                } else {
                    try {
                        JSONObject v2_1 = new JSONObject();
                        v0_4.b(v2_1, v0_4.a);
                        v1_1.put("header", v2_1);
                        String v2_2 = v0_4.a(v1_1.toString(), String.valueOf(System.currentTimeMillis()));
                        com.bytedance.frameworks.core.thread.c.a();
                        com.bytedance.frameworks.core.thread.c.a(new n(v0_4, v1_1, v2_2));
                    } catch (Throwable v0) {
                    }
                }
            }
            Iterator v2_3 = j.iterator();
            while (v2_3.hasNext()) {
                if (v2_3.next() == null) {
                    continue;
                }

                v1_1.toString();
            }
            return;
        } catch (Throwable v0) {
            label_65:
            v0.printStackTrace();
            return;
        }
    }

    private static String b() {
        StringBuilder v1 = new StringBuilder();
        StackTraceElement[] v2 = Looper.getMainLooper().getThread().getStackTrace();
        int v3 = v2.length;
        int v0;
        for (v0 = 0; v0 < v3; ++v0) {
            v1.append("  at " + v2[v0].getClassName()).append(".").append(v2[v0].getMethodName()).append("(").append(v2[v0].getFileName()).append(":").append(v2[v0].getLineNumber()).append(")\n");
        }

        return v1.toString();
    }
}

